package com.kady.Dao.impl;

import com.kady.Dao.OrderDao;
import com.kady.entity.*;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/27
 * \* Time: 10:48
 * \* 千峰JAVA2101班
 * \* Description: 订单Dao执行类
 * \
 */
public class OrderDaoImpl extends BaseDao implements OrderDao {
    /**
     * 查询订单列表
     */
    @Override
    public List<Map<String,Object>> selectOrderList() throws SQLException {
        String sql = "SELECT od.order_id orderId,\n" +
                "\tod.total_count totalCount,\n" +
                "\tod.order_total_price totalPrice,\n" +
                "\tod.order_time orderTime,\n" +
                "\tod.is_ordered isOrdered,\n" +
                "\tod.user_id userId,\n" +
                "\ttb.table_id tableId,\n" +
                "\ttb.table_name tableName FROM t_order od INNER JOIN t_table tb ON od.`table_id` = tb.`table_id`;";
        List<Map<String,Object>> res = selectInnerList(sql);
        return res;

    }

    /**
     * 通过ID查询单个订单
     */
    @Override
    public Order selectById(String orderId) throws SQLException {
        String sql = "SELECT order_id orderId,\n" +
                "\ttotal_count totalCount,\n" +
                "\torder_total_price totalPrice,\n" +
                "\torder_time orderTime,\n" +
                "\tis_ordered isOrdered,\n" +
                "\ttable_id tableId,\n" +
                "\tuser_id userId FROM t_order WHERE order_id = ?";
        Order order = selectOne(Order.class, sql, orderId);
        return order;
    }

    /**
     * 修改订单状态
     */
    @Override
    public long updateStatus(Object...objs) throws SQLException {
        String sql = "UPDATE t_order SET is_ordered = ? WHERE order_id = ?;";
        long res = update(sql, objs);
        return res;
    }

    /**
     * 获取订单详情
     */
    @Override
    public List<Map<String, Object>> selectDetailList(String orderId) throws SQLException {
        String sql = "SELECT od.order_detail_id detailId,\n" +
                "\tod.order_id orderId,\n" +
                "\t`count`,\n" +
                "\tfd.food_id foodId,\n" +
                "\tfd.food_name foodName,\n" +
                "\tfd.price price,\n" +
                "\tfd.vip_price vipPrice FROM order_detail od INNER JOIN food fd ON od.`food_id` = fd.`food_id` WHERE order_id = ? ";
        List<Map<String, Object>> maps = selectInnerList(sql, orderId);
        return maps;
    }

    /**
     * 计算普通总价 和 会员总价
     */
    @Override
    public Map countPrice(String orderId) throws SQLException {
        String sql = "SELECT SUM(`count`*fd.price) AS totalPrice,\n" +
                "\tSUM(`count`*fd.vip_price) AS vipTotalPrice \n" +
                "\tFROM order_detail od INNER JOIN food fd ON od.`food_id` = fd.`food_id` WHERE order_id = ?;";
        Map resMap = selectMapOne(sql,orderId);
        return resMap;
    }

    /**
     * 菜品删除时进行判断，统计订单详情中是否已有菜品在决定是否删除菜品
     */
    @Override
    public long countDetailsById(String foodId) throws SQLException {
        String sql = "SELECT count(1) from order_detail WHERE food_id = ?";
        long res = selectCount(sql, foodId);
        return res;
    }

    /**
     * 创建订单
     */
    @Override
    public long addOrder(String orderId,User user, String tableId, Cart cart) throws SQLException {
        String sql = "INSERT INTO t_order (order_id,total_count,order_total_price,order_time,is_ordered,table_id,user_id) VALUES (?,?,?,?,?,?,?) ";
        long res = update(sql, orderId,cart.getTotalCount(),cart.getTotalPrice(),new Date(),0,tableId,user.getUser_id());
        return res;
    }

    /**
     * 添加订单详情的菜品项
     */
    @Override
    public void addOrderDeatilItem(String orderId,String foodId, CartItem cartItem) throws SQLException {
        String sql = "INSERT INTO order_detail (order_id,food_id,count) VALUES (?,?,?) ";
        update(sql,orderId,foodId,cartItem.getCount());
    }

    /**
     * 使用用户Id查询 未结账的 订单
     */
    @Override
    public Order selectOrderByUserId(int user_id) throws SQLException {
        String sql = "SELECT order_id orderId,\n" +
                "\ttotal_count totalCount,\n" +
                "\torder_total_price totalPrice,\n" +
                "\torder_time orderTime,\n" +
                "\tis_ordered isOrdered,\n" +
                "\tuser_id userId FROM t_order WHERE user_id = ? AND is_ordered = ? ";
        Order res = selectOne(Order.class,sql, user_id,0);
        return res;
    }
}
